package com.jinmdz.fmis.api.wrapper;

import com.jinmdz.fmis.core.base.BaseBean;
import com.jinmdz.fmis.core.constant.Constants;
import com.jinmdz.fmis.core.util.JacksonUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * HttpServletResponse封装处理类
 *
 * @author LiCongLu
 * @date 2019-12-20 09:59
 */
@Component
public class ResponseWrapper {

    /**
     * 请求响应下载Excel文件
     *
     * @param response 响应对象
     * @param workbook XSSFWorkbook
     * @param filename 下载文件路径
     * @return
     * @author LiCongLu
     * @date 2020-01-03 14:24
     */
    public <T extends BaseBean> void downloadXlsx(HttpServletResponse response, XSSFWorkbook workbook, String filename) {
        try {

            filename = URLEncoder.encode(filename, Constants.UTF8) + ".xlsx";

            //设置响应头
            response.setContentType("application/octet-stream;charset=" + Constants.UTF8);
            response.setCharacterEncoding(Constants.UTF8);
            response.setHeader("Content-Disposition", "attachment;filename=" + filename);
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);

            try {
                //获取输出流
                OutputStream stream = response.getOutputStream();
                //用文档写输出流
                workbook.write(stream);
                //刷新输出流
                stream.flush();
            } catch (Exception e) {
                // e.printStackTrace();
            }
        } catch (Exception e) {
            // e.printStackTrace();
        }
    }

    /**
     * 请求响应下载文件
     *
     * @param response 响应对象
     * @param file     下载文件
     * @param filename 下载文件路径
     * @return
     * @author LiCongLu
     * @date 2020-05-21 13:08
     */
    public <T extends BaseBean> void downloadFile(HttpServletResponse response, File file, String filename) {
        try {

            filename = URLEncoder.encode(filename, Constants.UTF8);

            //设置响应头
            response.setContentType("application/octet-stream;charset=" + Constants.UTF8);
            response.setCharacterEncoding(Constants.UTF8);
            response.setHeader("Content-Disposition", "attachment;filename=" + filename);
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);

            try {
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    //获取输出流
                    OutputStream stream = response.getOutputStream();

                    int i = bis.read(buffer);
                    while (i != -1) {
                        stream.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    //刷新输出流
                    stream.flush();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            } catch (Exception e) {
                // e.printStackTrace();
            }
        } catch (Exception e) {
            // e.printStackTrace();
        }
    }

    /**
     * 请求响应返回Json字符串
     *
     * @param response 响应对象
     * @param bean     返回数据实体
     * @return
     * @author LiCongLu
     * @date 2019-12-20 10:04
     */
    public <T extends BaseBean> void json(HttpServletResponse response, T bean) {
        try {
            //设置编码格式
            response.setContentType("text/plain;charset=" + Constants.UTF8);
            response.setCharacterEncoding(Constants.UTF8);

            try {
                PrintWriter writer = response.getWriter();
                writer.write(JacksonUtil.obj2Json(bean));
                writer.flush();
            } catch (IOException e) {
                // e.printStackTrace();
            }

        } catch (Exception e) {
            // e.printStackTrace();
        }
    }
}
